🏠 Home

Terraform Crash Course - 1 Hour to Infrastructure as Code

βœ… 1: Welcome & Agenda

Title: Terraform Crash Course – 1 Hour to Infrastructure as Code
Subtitle: Your fast path to provisioning like a pro

πŸ“Œ Agenda

  1. What is Infrastructure as Code?
    Why engineers are replacing manual setups with declarative automation.
  2. Why Terraform?
    A quick look at what makes Terraform the industry standard.
  3. Terraform Core Concepts
    Providers, resources, state, and how it all works together.
  4. Hands-on Demo: Local Resource
    Your first .tf file: provision a file on your system.
  5. Terraform State & Lifecycle
    Understand how Terraform tracks what’s deployed β€” and what needs to change.
  6. Variables & Outputs
    Make your code more dynamic and reusable.
  7. Wrap-Up & Next Steps
    Best practices and where to go after this session.

🧱 2: The Problem with Manual Infrastructure

Title: Manual Infrastructure: What's the Problem?
πŸ”§ Manual setup used to be the norm β€” clicking around in cloud consoles, tweaking config files, spinning up servers by hand.

But as environments grew more complex, this approach began to break down:

ChatGPT Image Jul 16, 2025, 03_52_40 PM.png

🚨 Challenges with manual provisioning:

  • ❌ Hard to Reproduce
    No record of what was done β€” you hope the next person clicks the same buttons in the right order.
  • ⚠️ Prone to Human Error
    One misconfigured setting can break a production system.
  • πŸ‘₯ Poor Collaboration
    Knowledge is trapped in someone's head or in tribal processes, not in code.
  • πŸ“ˆ Difficult to Scale
    Spinning up identical environments (e.g., staging, test, dev) becomes slow and inconsistent.

☁️ 3: Infrastructure as Code (IaC)

Title: What is Infrastructure as Code?
Infrastructure as Code (IaC) means managing your servers, databases, networks, and other infrastructure just like application code β€” using files that are versioned, shareable, and automated.

πŸ“ Key Principles

  • Define Infrastructure Using Code
    Instead of clicking through cloud consoles, you write declarative config files that describe the infrastructure you want.
  • Version-Controlled
    Store infrastructure in Git. Review it. Track changes. Roll back if needed β€” just like you would with app code.
  • Reproducible & Declarative
    You define what you want (not how), and tools like Terraform make it so β€” across staging, dev, prod.
  • Automated Provisioning
    Spin up complete environments in minutes, consistently and safely, with minimal manual effort.

🧠 Analogy

Think of it like a Dockerfile β€” but for your entire infrastructure stack.

πŸ’‘ IaC helps eliminate β€œsnowflake environments” and turns infrastructure into reliable, testable, reviewable artifacts.

πŸ”§ 4: Why Terraform?

Title: Why Choose Terraform?
Terraform is one of the most widely adopted tools for managing infrastructure as code β€” and for good reason. It strikes a powerful balance between flexibility, scalability, and ease of use.

πŸ’‘ Cloud-Agnostic

Use the same tool and workflow to manage resources across AWS, Azure, GCP, and more β€” or even across multiple providers at the same time.

β€œWrite once, apply anywhere.”

🧱 Declarative Syntax (HCL)

You describe what infrastructure you want (not how to build it), using Terraform’s human-friendly configuration language (HCL).

πŸ” Reusable Modules

Define infrastructure components (e.g., VPCs, databases, EC2s) once and reuse them across environments, teams, or projects.

Write less, scale more.

πŸ“ State Tracking & Drift Detection

Terraform knows the difference between what exists and what should exist, helping detect configuration drift and manage changes safely.

πŸš€ Vibrant Ecosystem

Thousands of ready-made modules and providers on the Terraform Registry β€” from HashiCorp and the community.

Speeds up onboarding, prototyping, and production deployments.

🧠 Terraform lets you scale with confidence β€” across clouds, teams, and time.

🧩 5: Terraform Architecture

Title: Terraform’s Architecture
Terraform follows a simple but powerful workflow to manage infrastructure consistently and predictably.

πŸ› οΈ Core Workflow

  • You write .tf files
    Infrastructure is described using HCL (HashiCorp Configuration Language).
  • terraform plan
    Terraform compares your desired state (from .tf files) to what already exists and shows what will change β€” without making any changes yet.
  • terraform apply
    Executes the plan and makes the changes in your cloud environment β€” creating, modifying, or deleting resources.

πŸ“¦ State Management

  • State File (terraform.tfstate)
    Terraform keeps a snapshot of the current infrastructure state to detect drift and decide what to change.

πŸ”Œ Providers

  • Terraform uses providers (AWS, Azure, GCP, local, etc.) to interact with the target platform’s APIs.

Example: AWS provider converts .tf config into real EC2 instances, S3 buckets, etc.

ChatGPT Image Jul 16, 2025, 07_44_26 PM.png

🧠 This architecture is what lets Terraform manage real-world infrastructure using simple config files.

πŸ–₯️ 6: Installing Terraform

Title: Installing Terraform (Quick Guide)

  • Mac:
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
  • Windows: choco install terraform
  • Linux: Download from terraform.io

πŸ‘€ Check: terraform -version

βš™οΈ 7: Core Concepts

Title: Terraform Core Concepts
To use Terraform effectively, you only need to understand a few foundational building blocks. These concepts are simple, but incredibly powerful when combined.

🧩 Providers

Plugins that let Terraform talk to specific platforms and services.
Examples: AWS, Azure, Google Cloud, Kubernetes, Docker, Local.
Terraform gives you a small set of concepts to master β€” and with them, you can manage anything from storage buckets to full ML workflows on GCP.

provider "google" {
  project = "ml-infra-demo"
  region  = "us-central1"
}

🧱 Resources

The actual infrastructure you want to create β€” servers, buckets, files, etc.

πŸ“¦ Google Cloud Storage (GCS):

resource "google_storage_bucket" "data_bucket" {
  name     = "ml-pipeline-data-bucket"
  location = "US"
}

πŸ€– Kubeflow Pipelines (via GKE):

resource "google_container_cluster" "kfp_cluster" {
  name     = "kfp-cluster"
  location = "us-central1"

  initial_node_count = 1

  node_config {
    machine_type = "e2-standard-2"
  }
}

πŸ› οΈ Variables

Make your configs dynamic and reusable.

variable "bucket_name" {
  default = "ml-data-bucket"
}

# Use with:
name = var.bucket_name

♻️ Modules

Group related resources into reusable components.
Great for DRY code and scaling to real projects.

module "gcs_bucket" {
  source     = "./terraform-google-modules/cloud-storage/google"
  version    = "~> 3.0"
  name       = var.bucket_name
  project_id = var.project
  location   = "US"
}

πŸ“‚ State

Terraform uses a .tfstate file to track what it deployed.

  • Helps detect configuration drift.
  • In GCP, use remote state via Cloud Storage for team collaboration.
terraform {
  backend "gcs" {
    bucket = "terraform-state-storage"
    prefix = "ml-infra"
  }
}

🧠 Master these five and you can manage any infrastructure at scale.

πŸ’» 8: Hands-On: Local File Demo

Key Learning Objectives:
This is a progressive Terraform tutorial/learning repository that demonstrates core Terraform concepts through hands-on examples using the local provider to create files. Each numbered directory represents a learning module that builds upon previous concepts:

πŸ“˜ Project Code

Dive into the complete code examples here:
πŸ”— https://github.com/Nempickaxe/terraform_basics

πŸ” Run:

terraform init 
terraform plan 
terraform apply

πŸ“‚ 9: Terraform Lifecycle

Title: Terraform Workflow
Terraform uses a clean, predictable workflow to help you safely manage infrastructure β€” from creation to teardown.

πŸ”„ Step-by-Step Workflow:

1️⃣ terraform init

Initializes the working directory for Terraform.

  • Downloads the required provider plugins (e.g., Google Cloud, AWS).
  • Creates a .terraform/ folder locally.
  • Run this once when starting a new project or after adding providers/modules.
terraform init

2️⃣ terraform plan

Shows you exactly what Terraform will do β€” without making any changes.

  • Compares your code (.tf files) with existing infrastructure (state).
  • Outputs a preview of additions, changes, or deletions.
terraform plan

βœ… Use this to double-check before applying β€” catch mistakes early.

3️⃣ terraform apply

Executes the changes described in the plan.

  • Creates or modifies real infrastructure to match your config.
  • Will prompt for confirmation before proceeding.
terraform apply

⚠️ You can also use -auto-approve, but not recommended for production.

4️⃣ terraform destroy

Safely tears down all resources defined in your configuration.

  • Cleans up everything created by Terraform.
  • Useful for dev/test environments or cleanup tasks.
terraform destroy

🧠 Tip: Treat your Terraform workflow like git β€” plan before apply, and destroy with intention.

πŸ“¦ β€œWrite code β†’ Plan it β†’ Apply it β†’ Revert when needed.”

πŸ“„ 10: Terraform State

Title: What is terraform.tfstate?
Terraform uses a special file to keep track of what it created, updated, or destroyed β€” so it knows exactly what’s real, and what’s just in code.

πŸ“˜ terraform.tfstate is:

  • A JSON file that maps your desired state (from .tf files) to the actual infrastructure on your cloud provider.
  • Stores metadata like resource names, IDs, IPs, and relationships.
  • Used during plan and apply to determine what needs to change.

πŸ” Why It Matters

  • Enables drift detection:
    If someone makes a manual change outside of Terraform, the state will tell you something’s out of sync.
  • Powers safe updates:
    Terraform uses the state to ensure it doesn’t accidentally destroy or recreate things you didn’t intend.
  • Supports incremental changes:
    Only the differences are applied β€” no full redeploy needed.

🚫 Best Practices

  • Never edit the state file manually
    (Unless you really know what you’re doing.)
  • Don’t commit it to Git
    It may contain sensitive data like IPs, secrets, or credentials.
  • Use remote state for teams
    (e.g., GCS bucket with locking to avoid conflicts)

🧠 Think of terraform.tfstate as Terraform’s memory β€” without it, it has no idea what it built.

πŸ” 11: Variables & Outputs

Title: Reusability with Variables
Hardcoding values is fine β€” until you need to scale, reuse, or collaborate. Terraform lets you make your configurations flexible and dynamic using variables and outputs.

πŸ”§ Variables

Variables let you parameterize your infrastructure configs.

variable "file_content" {
  default = "Hello Team!"
}
  • Can be reused across multiple resources
  • Support types: string, number, bool, list, map, and more
  • Defined in variables.tf, values passed via:
    • terraform.tfvars
    • -var CLI flags
    • Environment variables

πŸ“€ Outputs

Outputs let you expose useful info from your infrastructure β€” like IP addresses, bucket URLs, or filenames.

output "file_path" {
  value = local_file.hello_file.filename
}
  • Displayed after terraform apply
  • Can be referenced by other modules or used in automation scripts

🎯 Pro Tip: Create a terraform.tfvars file to override default values cleanly:

file_content = "Hello from the team!"

🧠 Variables make your code flexible. Outputs make it visible.

🧠 12: Best Practices

Title: Terraform Best Practices

βœ… Always use terraform plan before apply
βœ… Store state securely (remote backend for real infra)
βœ… Use variables for anything configurable
βœ… Group logic into modules for reusability
βœ… Keep secrets out of .tf files

🎯 13: What You Learned

Title: In Just One Hour You Learned:

  • What is IaC & Terraform?
  • Terraform syntax & architecture
  • How to write a working .tf config
  • Terraform lifecycle & state
  • Variables and outputs

πŸ‘ You’ve now deployed infrastructure as code!